import SystemUtil from 'utils/systemUtil';

import moment from 'moment';

const _formatData = (value, dataType) => {
  if (value === undefined || value === null) {
    return value;
  }

  if (dataType === 'number') {
    if (value !== '') {
      return Number(value);
    }
  } else if (dataType === 'string') {
    return value + '';
  } else if (dataType === 'boolean') {
    if (value === '') {
      return '';
    }
    if (value === 'false') {
      return false;
    }
    return Boolean(value);
  } else if (dataType === 'date') {
    if (moment(value).isValid()) {
      return moment(value).format('YYYY-MM-DD');
    }
  }

  return value;
};

export default class base {
  initObject(obj = {}, propertyList = [], hasNull = true, defaultValue = '', hasTrim = false) {
    _.forEach(propertyList, property => {
      let value = _.get(obj, property);
      if (SystemUtil.isEmpty(value)) {
        if (!hasNull) {
          return;
        }
        value = defaultValue;
      } else {
        if (hasTrim && typeof value === 'string') {
          value = value.trim();
        }
      }
      _.set(this, property, value);
    });
  }

  /**
   * 设置请求参数数据
   * @param {*} postData
   * @param {*} propertyList 
   */
  setPostData(postData, propertyList = [], hasNullData = false, hasTrim = false) {
    _.forEach(propertyList, property => {
      let value = _.get(this, property);
      if (value === undefined || value === '') {
        if (!hasNullData) {
          return;
        }
        value = '';
      } else {
        if (typeof value === 'string') {
          if (hasTrim) {
            value = value.trim();
          }
        }
      }

      _.set(postData, property, value);
    });
  }

  resetObject(propertyList = [], defaultValue) {
    _.forEach(propertyList, property => {
      _.set(this, property, defaultValue);
    });
  }

  setOldData(propertyList = []) {
    _.forEach(propertyList, property => {
      let value = _.get(this, property);
      value = _.cloneDeep(value);
      _.set(this, `_old.${property}`, value);
    });
  }

  isChange(propertyList = []) {
    for (let i = 0; i < propertyList.length; i++) {
      const property = propertyList[i];
      let oldValue = this.getOldData(property);
      let value = this.getData(property);
      if (!_.isEqual(value, oldValue)) {
        return true;
      }
    }
    return false;
  }

  getOldData(property, dataType) {
    if (property === undefined || property === '') {
      return undefined;
    }

    let value = _.get(this, `_old.${property}`);
    value = _formatData(value, dataType);
    return value;
  }

  resetOldData(propertyList) {
    _.forEach(propertyList, property => {
      let oldValue = this.getOldData(property);
      oldValue = _.cloneDeep(oldValue);
      _.set(this, property, oldValue);
    });
  }

  getData(property, dataType = '') {
    if (property === undefined || property === '') {
      return undefined;
    }

    let value = _.get(this, property);
    if (dataType === '') {
      return value;
    }
    value = _formatData(value, dataType);
    if (value === undefined || value === null) {
      return '';
    }
    return value;
  }

  formatCount(fields) {
    _.forEach(fields, field => {
      let value = this[field];
      if (typeof value !== 'number') {
        return;
      }

      if (value < 0) {
        this[field] = 0;
      }
    });
  }

  checkRequired(propertyList = []) {
    for (let i = 0; i < propertyList.length; i++) {
      let property = propertyList[i];
      let value = this[property];
      if (SystemUtil.isEmpty(value)) {
        return property;
      }
    }
    return '';
  }

  setDatetimeData(obj = {}, propertyList = []) {
    _.forEach(propertyList, property => {
      let time = obj[property];
      if (SystemUtil.isEmpty(time)) {
        time = '';
      } else {
        time = moment(time).valueOf();
      }
      this[property] = time;
    });
  }

  getDatetimeValue(property, formatter = 'YYYY-MM-DD') {
    let time = this[property];
    if (SystemUtil.isEmpty(time)) {
      return '';
    }

    return moment(time).format(formatter);
  }
}
